package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.WoExecTjController;
import com.antoco.operation.bigscreen.model.dto.WoExecTjDTO;
import com.antoco.operation.bigscreen.model.entity.WoExecTj;
import com.antoco.operation.bigscreen.mapper.WoExecTjMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.WoExecTjService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/wo-exec-tj")
@RestController
public class WoExecTjControllerImpl implements WoExecTjController {

    @Autowired
    private WoExecTjService woExecTjService;

    @Autowired
    private WoExecTjMapper woExecTjMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public WoExecTjDTO save(@Validated @RequestBody WoExecTjDTO woExecTjDTO) {
        WoExecTj woExecTj = woExecTjMapper.asEntity(woExecTjDTO);
        woExecTjService.save(woExecTj);
        channelDataService.pushAllLists(this.getClass().getName());
        return woExecTjDTO;
    }

    @Override
    @GetMapping("/{id}")
    public WoExecTjDTO findById(@PathVariable("id") String id) {
        WoExecTj woExecTj = woExecTjService.findById(id).orElse(null);
        return woExecTjMapper.asDTO(woExecTj);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        woExecTjService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<WoExecTjDTO> list() {
        return woExecTjMapper.asDTOList(woExecTjService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<WoExecTjDTO> pageQuery(Pageable pageable) {
        Page<WoExecTj> woExecTjPage = woExecTjService.findAll(pageable);
        List<WoExecTjDTO> dtoList = woExecTjPage
                .stream()
                .map(woExecTjMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, woExecTjPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public WoExecTjDTO update(@RequestBody WoExecTjDTO woExecTjDTO, @PathVariable("id") String id) {
        WoExecTj woExecTj = woExecTjMapper.asEntity(woExecTjDTO);
        woExecTjMapper.asDTO(woExecTjService.update(woExecTj, id));
        channelDataService.pushAllLists(this.getClass().getName());
        return woExecTjDTO;
    }
}